<?php
namespace app\api\middleware;

use think\Request;
use think\facade\Log;
use Firebase\JWT\JWT;
use think\facade\Db;
use app\admin\model\SystemOperationRecord;
header("content-type:text/html;charset=utf-8");

/**
 * 检测api
 * Class CheckApi
 *
 * @package app\api\middleware
 */
class CheckApi
{
    
    use \app\common\traits\JumpTrait;

    public function handle(Request $request, \Closure $next)
    {
        if (! empty($request->_UID)) {
            
            $admin = Db::name("system_user")->where("user_id", $request->_UID)->find();
        }
        
        $requestInfo = [
            'ip' => $request->ip(),
            'method' => $request->method(),
            'host' => $request->host(),
            'uri' => $request->url()
        ];
        
        $time = date(' c ');
        $logInfo = [
            "---------------------------------------------------------------\r\n[{$time}] {$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}",
            '[ info ] [ HEADER ] ' . var_export($request->header(), true),
            '[ info ] [ PARAM ] ' . var_export($request->param(), true),
            '---------------------------------------------------------------'
        ];
        $logInfo = implode(PHP_EOL, $logInfo) . PHP_EOL;
        
        Log::write($logInfo, 'info');
        
        return $next($request);
    }
}